package com.file.system.service.impl.system;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.system.WebFilterWord;
import com.file.system.mapper.system.WebFilterWordMapper;
import com.file.system.plugin.shiro.ShiroTools;
import com.file.system.plugin.validated.FilterWord;
import com.file.system.service.system.IWebFilterWordService;
import com.file.system.tools.page.DP;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 过滤词汇 实现
 *
 * @author jch
 * @since 2023-07-17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WebFilterWordServiceImpl implements IWebFilterWordService {

    @Autowired
    private WebFilterWordMapper mapper;


    //添加过滤词汇
    @FilterWord
    public boolean save(WebFilterWord webFilterWord) {
        webFilterWord.setId(IdUtil.getSnowflakeNextId());
        return SqlHelper.retBool(mapper.save(webFilterWord));
    }


    //删除过滤词汇
    @FilterWord
    public boolean removeById(@Param("id") Long id) {
        return SqlHelper.retBool(mapper.removeById(new WebFilterWord().setId(id)));
    }

    //通过ids删除过滤词汇
    public boolean removeByIds(List<Long> ids) {
        UpdateWrapper<WebFilterWord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_del", 1);
        updateWrapper.set("delete_by", ShiroTools.getId());
        updateWrapper.set("delete_name", ShiroTools.getName());
        updateWrapper.set("delete_time", LocalDateTime.now());
        updateWrapper.in("id", ids);


        int state = mapper.update(updateWrapper);
        if (state < 1) {
            return false;
        }
        return true;
    }

    //编辑过滤词汇
    @FilterWord
    public boolean updateById(@Param("et") WebFilterWord webFilterWord) {
        if (ObjectUtil.isEmpty(webFilterWord.getId())) {
            return false;
        }
        return SqlHelper.retBool(mapper.updateById(webFilterWord));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<WebFilterWord> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //添加过滤词汇(批量)
    public boolean saveBatch(@Param("list") List<WebFilterWord> webFilterWordList) {
        if (webFilterWordList.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(webFilterWordList));
    }

    //查询过滤词汇
    @DS("slave")
    public List<WebFilterWord> list(@Param("ew") QueryWrapper<WebFilterWord> query) {
        return mapper.select(query);
    }

    //通过ids查询过滤词汇集合
    @DS("slave")
    public List<WebFilterWord> listByIds(List<Long> ids) {
        QueryWrapper<WebFilterWord> query = new QueryWrapper<>();
        query.in("a.id", ids);
        query.orderByDesc("a.id");
        return mapper.select(query);
    }

    //查询所有非删除状态过滤词汇
    @DS("slave")
    public List<WebFilterWord> listAll() {
        QueryWrapper<WebFilterWord> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        query.orderByDesc("a.id");
        return mapper.select(query);
    }

    //查询过滤词汇分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<WebFilterWord> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebFilterWord> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebFilterWord> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //通过id查询过滤词汇
    @DS("slave")
    public WebFilterWord getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询过滤词汇
    @DS("slave")
    public WebFilterWord getOne(@Param("ew") QueryWrapper<WebFilterWord> query) {
        List<WebFilterWord> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询过滤词汇数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebFilterWord> query) {
        return mapper.selectCount(query);
    }
}
